package com.software.project.timer;

import com.software.project.entity.UserInfo;
import com.software.project.repository.UserInfoRepository;
import com.software.project.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;

@Configuration
@EnableScheduling
@Slf4j
public class StudyTimeScheduleTask {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Scheduled(cron="0 59 23 * * ?")//每天23点59执行
    private void configureTasks(){
        log.info("重置每日学习时长");
        //拿到所有用户
        ArrayList<UserInfo> list=new ArrayList<UserInfo>();
        list=(ArrayList<UserInfo>) userInfoRepository.findAll();
        for(int i=0;i<list.size();i++){
            UserInfo userInfo=list.get(i);
            //用户今日学习置0
            userInfo.setTodayLearningTime(0);
            //更新
            userInfoService.createUserInfo(userInfo);
        }
        log.info("每日学习时长重置完成");
    }
}
